package com.jin.spring_boot_learn;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.atomic.AtomicInteger;

public class retryContainer {

    public static class Producer implements Runnable {
        private static AtomicInteger count = new AtomicInteger(0);

        private BlockingQueue queue;

        public Producer(BlockingQueue queue) {
            this.queue = queue;

        }

        @Override
        public void run() {
            while (true) {
                try {
                    DelayObject object = new DelayObject(count.incrementAndGet()+"abc",
                             5000);
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//设置日期格式
                    System.out.println("Put object   "+  object+df.format(new Date()));
                    queue.put(object);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }



    public static class Consumer implements Runnable {

        private BlockingQueue<DelayObject> queue;

        public Consumer(BlockingQueue<DelayObject> queue) {
            this.queue = queue;

        }

        @Override
        public void run() {
            while (true) {
                try {
                    DelayObject obj = queue.take();
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//设置日期格'
                    System.out.println("get----------------------------- "+obj+df.format(new Date()));
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }



    public static void main(String[] args) {
        DelayQueue<DelayObject> delayeds = new DelayQueue<>();

        Thread thread1 = new Thread(new Producer(delayeds));

        Thread thread2 = new Thread(new Consumer(delayeds));

        thread1.start();

        thread2.start();





    }
}
